﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zcu.PanelComposer.ViewModels.Core;
using Caliburn.Micro;
using Zcu.PanelComposer.ViewModels.Events;
using Zcu.PanelComposer.Models.Model;
using System.Windows;
using Zcu.PanelComposer.Interfaces;
using System.ComponentModel.Composition;
using System.Windows.Input;
using System.Windows.Media;
using System.Globalization;

namespace Zcu.PanelComposer.ViewModels.Pages
{
    /// <summary>
    /// Kresleni pudorysu.
    /// </summary>
    [Export(typeof(IModule))]
    public class BaseViewModel : NavigateScreen
    {
        #region Fields

        /// <summary>
        /// Reference na logger.
        /// </summary>
        private readonly ILog _log = LogManager.GetLog(typeof(ProjectDetailViewModel));

        /// <summary>
        /// Reference na aktivni projekt.
        /// </summary>
        private Project _SelectedProject;

        #endregion // Fields

        #region Properties

        [Import]
        public IRepository Repository { get; private set; }

        /// <summary>
        /// Reference na aktivni projekt.
        /// </summary>
        public Project SelectedProject
        {
            get { return _SelectedProject; }
            set
            {
                if (_SelectedProject != value) _SelectedProject = value;
                NotifyOfPropertyChange(() => SelectedProject);
            }
        }

        private PointCollection _PolyCollection;
        public PointCollection PolyCollection
        {
            get { return _PolyCollection; }
            set
            {
                if (_PolyCollection != value)
                    _PolyCollection = value;
                NotifyOfPropertyChange(() => PolyCollection);
            }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Konstruktor ViewModelu, prednastaveni pocatecnich hodnot.
        /// Nazev stranky (titulek), nazev modulu (kvuli navigaci) a poradni v menu.
        /// </summary>
        public BaseViewModel()
        {
            /// todo do resources
            DisplayName = "Base Detail";
            ModuleName = PageNames.Base;
            Order = 5;
            MenuVisible = false;
        }

        #endregion // Constructor

        #region Overrided methods

        /// <summary>
        /// Udalost, ktera je volana hned po vytvoreni screenu.
        /// </summary>
        public override void OnReconstruct()
        {
            base.OnReconstruct();

            // registrace zachycovani udalosti zmeny projektu.
            EventAggregator.Subscribe(this);
        }

        /// <summary>
        /// Called when activating.
        /// </summary>
        protected override void OnActivate()
        {
            base.OnActivate();

            if (Context != null && Context is Project)
            {
                SelectedProject = (Project)Context;
            }
            else
            {
                throw new Exception("Context is empty or not Project type");
            }

            LoadData();
        }

        #endregion

        #region Public methods

        /// <summary>
        /// Clears the action.
        /// </summary>
        public void ClearAction()
        {
            PolyCollection.Clear();
        }

        /// <summary>
        /// Akce, vyvolana stiskem tlacitka "Rename project".
        /// </summary>
        public void SaveAction()
        {
            var polygon = new PointCollection(ClippingEar.MakeClockOtherwise(PolyCollection))
                .ToString(CultureInfo.InvariantCulture);
            if (SelectedProject.BasePolygon != polygon)
            {
                SelectedProject.XamlTexture = null;
                SelectedProject.Drawing = null;
            }

            // ulozime jiz ve spravnem poradi do DB
            SelectedProject.BasePolygon = polygon;

            Repository.Commit();
            // ulozit do DB
            Navigate(PageNames.ProjectDetail);
        }

        /// <summary>
        /// Akce, vyvolana stiskem tlacitka "Draw base".
        /// </summary>
        public void BackAction()
        {
            Navigate(this.SourcePage);
        }

        #endregion // Public methods

        #region Private methods

        /// <summary>
        /// Loads the data.
        /// </summary>
        private void LoadData()
        {
            // nacist z DB
            var poly = PointCollection.Parse(SelectedProject.BasePolygon);
            PolyCollection = poly;
        }

        #endregion

        #region Handle events

        /// <summary>
        /// Zachyceni udalosti "aktivace projektu".
        /// </summary>
        /// <param name="message">The message.</param>
        public void Handle(ActiveProjectEvent message)
        {
            SelectedProject = (Project)message.Project;
        }

        #endregion
    }
}
